import java.util.HashMap;

public class 两数之和 {

    private static int[] num2Sum(int[] nums, int target){

        if(null==nums || nums.length < 1) return new int[]{};

        HashMap<Integer, Integer> tempMap = new HashMap<>();

        for(int i=0; i<nums.length; i++){

            int dif = target - nums[i];

            if(tempMap.containsKey(dif)){


                return new int[]{i, tempMap.get(dif)};

            }

            //加入map 缓存
            tempMap.put(nums[i], i);

        }


        return new int[]{};

    }
    
    public static void main(String[] args) {
        
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        int[] result = num2Sum(nums, target);
        if (result.length > 0) {
            System.out.println("[" + result[0] + ", " + result[1] + "]");
        } else {
            System.out.println("No two numbers found.");
        }

    }
}
